package com.kaisademo.zhxy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaisademo.zhxy.pojo.Student;
import com.kaisademo.zhxy.service.StudentService;
import com.kaisademo.zhxy.util.MD5;
import com.kaisademo.zhxy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "学生控制器")
@RestController
@RequestMapping("/sms/studentController")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @ApiOperation("删除一个或者多个学生信息")
    @DeleteMapping("/delStudentById")
    public Result delStudentById(
            @ApiParam("多个学生id的JSON")
            @RequestBody List<Integer> ids
    ){
        studentService.removeByIds(ids);
        return Result.ok();
    }

    @ApiOperation("增加或修改学生信息")
    @PostMapping("/addOrUpdateStudent")
    public Result addOrUpdateStudent(
            @ApiParam("JSON的student对象转换后台数据模型")
            @RequestBody Student student){
        if(student.getId() == null || student.getId() == 0){
            String password = MD5.encrypt(student.getPassword());
            student.setPassword(password);
        }
        studentService.saveOrUpdate(student);
        return Result.ok();
    }

    @ApiOperation("查询学生信息,分页带条件")
    @GetMapping("/getStudentByOpr/{pageNo}/{pageSize}")
    public Result getStudentByOpr(
            @ApiParam("页码数")
            @PathVariable(value = "pageNo") Integer pageNo,
            @ApiParam("页大小")
            @PathVariable(value = "pageSize") Integer pageSize,
            @ApiParam("查询条件转换后端数据模型")
            Student student
    ){
        Page<Student> studentPage = new Page<>(pageNo,pageSize);
        IPage<Student> studentIPage = studentService.getStudentByOpr(studentPage,student);
        return Result.ok(studentIPage);
    }
}
